package com.finger.userservice.controller;


import com.finger.commonutil.exception.YyghException;
import com.finger.commonutil.helper.JwtHelper;
import com.finger.modelservice.model.user.UserInfo;
import com.finger.modelservice.result.Result;
import com.finger.modelservice.result.ResultCodeEnum;
import com.finger.modelservice.vo.user.LoginVo;
import com.finger.modelservice.vo.user.UserAuthVo;
import com.finger.userservice.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 * 本接口用于前台用户模块
 */
@RestController
@RequestMapping("site/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("login")
    public Result loginUser(@RequestBody LoginVo loginVo){

        Map<String,Object> resultMap = userInfoService.login(loginVo);
        return Result.ok(resultMap);

    }

    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest req){

        try {
            String userId = JwtHelper.getUserId(req.getHeader("token"));

            UserInfo userInfo = userInfoService.getById(userId);
            Map<String, Object> param = userInfo.getParam();

            switch (userInfo.getAuthStatus()){
                case 0:
                    param.put("authStatusString","未认证");
                    break;
                case 1:
                    param.put("authStatusString","认证中");
                    break;
                case 2:
                    param.put("authStatusString","已认证");
                    break;
                default:break;
            }
            return Result.ok(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new YyghException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
    }

    @PostMapping("auth/userAuth")
    public Result userAuth(HttpServletRequest req,@RequestBody UserAuthVo userAuthVo){

        userInfoService.userAuth(req.getHeader("token"),userAuthVo);

        return Result.ok();
    }


}
